import { defineStore } from 'pinia'
import { resetRouter } from '@/router'
import { usePermissionStore } from './permission'

const store = () => {
  return {
    token: JSON.parse(localStorage.getItem('token')),
    name: JSON.parse(localStorage.getItem('name')),
    roles: JSON.parse(localStorage.getItem('roles')),
    hostname: JSON.parse(localStorage.getItem('hostname')), //主机名称
    diviceNo: JSON.parse(localStorage.getItem('diviceNo')), //激活码
    mac: JSON.parse(localStorage.getItem('mac')) //mac地址
  }
}

export const useUserStore = defineStore({
  id: 'user',
  store,
  actions: {
    loginStore(hostname, diviceNo) {
      localStorage.setItem("token", "edit");
      localStorage.setItem("roles", JSON.stringify(["edit"]));
      localStorage.setItem("name", "Edit");
      if (hostname && diviceNo) {
        localStorage.setItem("hostname", JSON.stringify(hostname));
        localStorage.setItem("diviceNo", JSON.stringify(diviceNo));
      }
      this.token = "edit"
      this.name = "Edit"
      this.roles = ["edit"]
      // this.hostname = ''
      // this.diviceNo = ''
    },
    logOut() {
      return new Promise((resolve, reject) => {
        const { ResetRoutes } = usePermissionStore()
        // localStorage.setItem("token", "");
        localStorage.setItem("roles", JSON.stringify([]));
        localStorage.setItem("name", "");
        this.token = ""
        this.name = ""
        this.roles = []
        ResetRoutes()
        resetRouter()
        resolve()
      })
    },
    GetUserInfo() {
      return new Promise((resolve, reject) => {
        resolve(this.roles)
      })
    }
  }
})